from django.db import models
from django.conf import settings
from django.urls import reverse

# Create your models here.
class ReviewUser(models.Model):
    user = models.OneToOneField(
        to=settings.AUTH_USER_MODEL,
        on_delete=models.CASCADE,
        primary_key=True
    )

    review_api_token = models.CharField(max_length=100, unique=True, default="")
    # Lazy created at the first time do review operation.
    review_cookie_file = models.FileField(max_length=100, blank=True, null=True)


class ReviewProfile(models.Model):
    user = models.ForeignKey(
        settings.AUTH_USER_MODEL,
        on_delete=models.CASCADE,
    )

    profile_name = models.CharField(max_length=100)

    # https://www.reviewboard.org/docs/manual/2.5/webapi/2.0/resources/review-request/#webapi2.0-review-request-resource
    review_target_people = models.CharField(max_length=100, blank=True, default="")
    review_target_groups = models.CharField(max_length=100, blank=True, default="")

    # default testing done
    review_testing_done = models.CharField(max_length=100, blank=True, default="")

    review_repository = models.CharField(max_length=100)
    review_branch = models.CharField(max_length=100)
    review_description_template = models.CharField(max_length=1000, blank=True, default="")

    def get_absolute_url(self):
        return reverse('review:profile-update', kwargs={'pk': self.pk})

    def __str__(self):
        return self.profile_name


class ReviewRecordGroup(models.Model):
    user = models.ForeignKey(
        settings.AUTH_USER_MODEL,
        on_delete=models.CASCADE,
    )


class ReviewRecord(models.Model):
    user = models.ForeignKey(
        settings.AUTH_USER_MODEL,
        on_delete=models.CASCADE,
    )

    review_profile = models.ForeignKey(
        ReviewProfile,
        on_delete=models.SET_NULL,
        null=True
    )

    review_group = models.ForeignKey(
        ReviewRecordGroup,
        on_delete=models.SET_NULL,
        null=True
    )

    review_id = models.IntegerField(blank=True, null=True, default=0)
    review_abs_url = models.CharField(max_length=100, blank=True, default="")
    final_patch = models.FileField(upload_to=f'upload/patches/')
    review_branch = models.CharField(max_length=100, blank=True, default="")
    review_summary = models.CharField(max_length=200, blank=True, default="")
    review_description = models.CharField(max_length=2000, blank=True, default="")
    review_testing_done = models.CharField(max_length=2000, blank=True, default="")
    review_target_people = models.CharField(max_length=100, blank=True, default="")
    review_target_groups = models.CharField(max_length=100, blank=True, default="")
    review_change_description = models.CharField(max_length=2000, blank=True, default="")
    ship_it_count = models.IntegerField(default=0)
    bugs = models.CharField(max_length=100, default='')
    commit_id = models.CharField(max_length=200, default='')

    public = models.BooleanField(default=False)
